package com.itheima.controller;

import com.itheima.DTO.CheckItemDTO;
import com.itheima.DTO.CheckItemPageQueryDTO;
import com.itheima.VO.CheckItemVO;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import com.itheima.service.ICheckgroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
@Api(tags = "检查项管理")
public class CheckItemController {
    @Autowired
    private CheckitemService checkitemService;
    @Autowired
    private ICheckgroupService checkgroupService;
    @PostMapping("/findPage")
    @ApiOperation("分页查询")
    public PageResult findPage(@RequestBody CheckItemPageQueryDTO checkItemPageQueryDTO){
        log.info("分页查询:{}",checkItemPageQueryDTO);
        PageResult pageResult = checkitemService.findPage(checkItemPageQueryDTO);
        return pageResult;
    }
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result add(@RequestBody CheckItemDTO checkItemDTO){
        log.info("新增检查项:{}",checkItemDTO);
        checkitemService.addCheckItem(checkItemDTO);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,null);
    }
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查项")
    public Result findById(@RequestParam("id") Integer id){
        CheckItemVO checkItemVO = checkitemService.findGetById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemVO);
    }
    @PostMapping("/edit")
    @ApiOperation("编辑检查项")
    public Result edit(@RequestBody CheckItemDTO checkItemDTO){
        log.info("编辑检查项:{}",checkItemDTO);
        checkitemService.edit(checkItemDTO);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
    }
    @GetMapping("/delete")
    @ApiOperation("删除检查项")
    public Result delete(@RequestParam("id") Integer id){
        log.info("删除检查项:{}",id);
        checkitemService.deleteItemById(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS,null);

    }
    @GetMapping("/findAll")
    @ApiOperation("查询所有检查项")
    public Result findAll(){
        List<CheckItem> checkItems = checkitemService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据套餐id查询检查组id")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam(value = "checkgroupId") Integer id){
        log.info("根据套餐id查询检查组id:{}",id);
        List<Integer> checkGroup =checkgroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,"查询成功",checkGroup);

    }

}
